import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

class Card {
    public String suit;//表示花色
    public String rank;//表示点数

    public Card(String suit, String rank) {
        this.suit = suit;
        this.rank = rank;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    public String getRank() {
        return rank;
    }

    public void setRank(String rank) {
        this.rank = rank;
    }

    @Override
    public String toString() {
        return this.suit+this.rank;
    }
}

public class Main {
    public static void main(String[] args) {
        //使用ArrayList表示一副扑克牌
        ArrayList<Card> deck = CreateDeck();
        System.out.println("原始牌组:"+deck);
        //洗牌
        //Collections.shuffle(deck);
        System.out.println("洗牌之后:"+deck);

        ArrayList<ArrayList<Card>> hands = new ArrayList<ArrayList<Card>>();//构成一个二维数组
        //先创建 3 个人手里的牌
        for (int i = 0; i < 3; i++) {
            ArrayList<Card> hand = new ArrayList<>();//每一个人的手牌
            hands.add(hand);//发到每个人手里
            //已经添加了3个元素进去，但还是长度为0的ArrayList
        }
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                //发牌是轮流的过程.
                ArrayList<Card> currentHand = hands.get(j);
                Card card = deck.remove(0);
                currentHand.add(card);
                //一次循环，相当于取出一张牌交给玩家
            }
        }
        //打印每个人的手牌
        for (int i = 0; i < 3; i++) {
            System.out.println("第"+i+"个人的手牌"+hands.get(i));
        }
    }

    public static void shuffle(ArrayList<Card> deck){
        //把整个ArrayList从后往前遍历
        //每次取一张牌，生成一个随机下标，用当前的拍和随机下标的牌进行交换
        Random random = new Random();
        for (int i = deck.size(); i >0 ; i--) {
            int j = random.nextInt(deck.size());//生成随机下标[0,deck.size)
            //交换操作
            Card tmp = new Card(deck.get(i).getSuit(), deck.get(i).getRank());//必须要确保tmp拿到i的下标，不能交换的时候发生修改
            deck.set(i, deck.get(j));
            deck.set(j, tmp);
        }
    }
    public static ArrayList<Card> CreateDeck() {
        //创建一副扑克牌
        ArrayList<Card> deck = new ArrayList<>();
        String[] suits = {"♠","♦","♥","♣"};
        //通过两层for循环，第一层先循环花色，第二层在循环点数
        //每个花色中，生成对应的点数
        for (String suit:suits){
            for (int i = 2; i <= 10; i++) {
                Card cardNum = new Card(suit,""+i);
                deck.add(cardNum);//添加到扑克牌中
            }

            Card cardJ = new Card(suit,"J");
            Card cardQ = new Card(suit,"Q");
            Card cardK = new Card(suit,"K");
            Card cardA = new Card(suit,"A");
            deck.add(cardJ);
            deck.add(cardQ);
            deck.add(cardK);
            deck.add(cardA);
        }
        return deck;
    }
}
